package com.homwee.factory;

//am start -n com.changhong.factory/.FactoryActivity
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Looper;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View.OnKeyListener;
import android.widget.Toast;
import android.os.SystemProperties;

import com.homwee.factory.util.BTManager;
import com.homwee.factory.pageData.PageData;
import com.homwee.factory.pageData.PageData0;
import com.homwee.factory.pageData.PageData1;
import com.homwee.factory.pageData.PageData10;
import com.homwee.factory.pageData.PageData2;
import com.homwee.factory.pageData.PageData3;
import com.homwee.factory.pageData.PageData4;
import com.homwee.factory.pageData.PageData5;
import com.homwee.factory.pageData.PageData6;
import com.homwee.factory.pageData.PageData7;
import com.homwee.factory.pageData.PageData8;
import com.homwee.factory.pageData.PageData9;
import com.homwee.factory.util.Contants;
import com.homwee.listMenu.ListMenu;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.tvap.common.comSettingInterface.MenuConfigManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;
import com.mediatek.twoworlds.tv.MtkTvBroadcastBase;
import com.mediatek.twoworlds.tv.MtkTvConfigBase;
import com.mediatek.twoworlds.tv.model.MtkTvChannelInfoBase;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.mediatek.twoworlds.factory.MtkTvFApiDisplayTest;
import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.tv.MtkTvUtil;
import com.mediatek.twoworlds.tv.common.MtkTvIntentBase;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;
import com.mediatek.wwtv.tvcenter.util.SaveValue;
import com.mediatek.twoworlds.tv.MtkTvConfig;
import com.mediatek.twoworlds.tv.MtkTvBroadcast;

import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;


public class FactoryActivity extends Activity {
	/** Called when the activity is first created. */
	public static String TAG = "FactoryActivity";
	// public static SystemManager factorySystemManager;
	// public UartColorTempListener notifyLisenter;
	private ListMenu mListMenu;
	private int curPageIndex = 0;
	private int ItemInitIndex = 0;
	public static Context mContext = null;
	public boolean isLcd = true;
	public boolean isSuportDtv = true;
	public static int DtvOrAtv;
	MenuConfigManager mCfg = MenuConfigManager.getInstance(null);
	// TvMiscManager mTvMiscManager = null;
	// TvChannelManager mTvChannelManager = null;
	MtkTvChannelInfoBase mMtkTvChannelInfoBase = null;
	MtkTvBroadcastBase mMtkTvBroadcastBase = null;
	MtkTvConfigBase mMtkTvConfigBase = null;
	AudioManager mAudioManager = null;
	// private TVManager mChTVManager =null;
	// private IATVPlayer mIATVPlayer =null;
	// private ATVChannelManager mATVChannelManager =null;
	private String[] strName;
	private PageData mPageData[];
	private Long time = 0L;
	SurfaceView mSurvaceView = null;

	private BTManager mBtManager;
	private BluetoothAdapter mBluetoothAdapter;
	
	String Keycodevalue="";
	String accessKeyCode="";

	public static final String mBrand = android.os.Build.BRAND;
	public static final String mProduct = ProductFun.getProduct();
	
	int[] mKeyCode = { 0, 0, 0 };

	private static final int MESSAGE_CHANGE_ID = 0x01;
	private int mChangeDigitMajor = 0;
	private int changeNum = 0;
	private final int WAIT_EXPIRE_TIME = 3000;
	/* zcj add for change project id */
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case MESSAGE_CHANGE_ID: {
				mChangeDigitMajor = 0;
			}
				break;
			default:
				break;
			}
		}

	};

		Handler btHandler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case BTManager.MSG_BT_DEVICE_FOUND:
				BluetoothDevice device = (BluetoothDevice) msg.obj;
				if (device == null || device.getName() == null) {
					break;
				}

				if (!device.getName().equalsIgnoreCase("null")
						) {
					if (!mBtManager.BTData.contains(device.getName())){
							mBtManager.BTData.add(device.getName());
					}
				}
				if (mBtManager.BTData.size() >= 3) {
					mBluetoothAdapter.cancelDiscovery();
				}

				break;
			default:
				break;
			}
		}
	};

	private void BTCheck(){
		mBtManager = BTManager.getInstance(mContext);
		mBluetoothAdapter = mBtManager.mBTAdapter;
		if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
			mBluetoothAdapter.enable();
		}

		mBtManager.setHandler(btHandler);
		mBtManager.registerBTReceiver();

		startSearch();
		
	}

	private void startSearch() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				if (mBluetoothAdapter != null) {
					mBluetoothAdapter.startDiscovery();
				}
			}
		}).start();
	}

	private void changeProjectID(int keycode) {
		mChangeDigitMajor++;
		if(mChangeDigitMajor>3){
			mChangeDigitMajor=1;
		}
		MenuItemData mData = mListMenu.getCurMenuItemData();
		int tempNum = keycode - KeyEvent.KEYCODE_0;
		if (mChangeDigitMajor == 1) {
			if (tempNum == 0) {
				changeNum = 1;
			} else {
				changeNum = tempNum;
			}
		} else if (mChangeDigitMajor == 2) {
			changeNum = changeNum * 10 + tempNum;
		} else if (mChangeDigitMajor == 3) {
			changeNum = changeNum * 10 + tempNum;
		}
		if (mData != null && mData.equals(mPageData1.projectID)) {
			mData.updateValue(changeNum);
		}
		mHandler.removeMessages(MESSAGE_CHANGE_ID);
		mHandler.sendEmptyMessageDelayed(MESSAGE_CHANGE_ID, WAIT_EXPIRE_TIME);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DisplayMetrics mDisplayMetrics = getResources().getDisplayMetrics();

		Log.d(TAG, "mDisplayMetrics.densityDpi:" + mDisplayMetrics.densityDpi
				+ ";" + "mDisplayMetrics.density:" + mDisplayMetrics.density
				+ ";");

		mContext = this;
		BTCheck();
		loadData();
		FactorySetting factorySetting=FactorySetting.getInstance(mContext);

		if(factorySetting.getAutoEnterFactory().equals("1")){
			Log.i(TAG,"auto enter factory");
			//set M mode
			if(mProduct.contains("ISDB")&&mBrand.equals("Philco")){
				Log.d(TAG,"boot on auto enter factory ,brand is pl");
				factorySetting.setPowerMode(2);
			}else {
				FactoryKeyReciver.setFactoryMMode(mContext, 1);
				factorySetting.setPowerMode(2);
				Intent intentForBarServer = new Intent(mContext, FactoryModeService.class);
				mContext.startService(intentForBarServer);
				FactoryModeMenu.refresh(mContext);
			}

			Intent mIntent =new Intent();
			mIntent.setAction("com.homwee.action.FactorySkipWizard");
			mIntent.addFlags(0x01000000);
			mContext.sendBroadcast(mIntent);
			Log.d("CHFactory","send skipWizard boardcast");
		}

		isAgingMode();
		keypadControl(1);

		if(TextUtils.isEmpty(Contants.FACTORY_PAGE2_CUSTOMER_PATH)) {
			Log.d(TAG,"start init DataPath");
			initDataPath(ComSettingManager.getInstance(this).getFactory().getDataIndex());
		}

		mListMenu = new ListMenu(mContext);
		setContentView(mListMenu);
		mListMenu.setVersion(this.getString(R.string.factory_software_version)
				+ getSWVersion() +"   DV: "+ComSettingManager.getInstance(this).getFactory().getDataVersion());
		mListMenu.setVersionDate(this.getString(R.string.factory_comfile_time)
				+ getSWDate());
		// updateOnePage(0);
		if(FactorySetting.isFactoryShowNoFunc()){
			curPageIndex = getIntent().getIntExtra("PageIndex", 10);//
		}else{
			curPageIndex = getIntent().getIntExtra("PageIndex", 8);
		}
		ItemInitIndex = getIntent().getIntExtra("ItemIndex", -1);// -1
		updateOnePage(curPageIndex, ItemInitIndex);
		mMtkTvChannelInfoBase = new MtkTvChannelInfoBase();
		mAudioManager = (AudioManager) mContext
				.getSystemService(Context.AUDIO_SERVICE);
		
		Log.v("tv", "keycode=" + getIntent().getIntExtra("KeyCode", -1));
		

		try {
			// zcj 2018-10-30 boolean preset =
			// mTvMiscManager.getATVPresetFlag();
			boolean preset = false;
			if (preset) {
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						// initChannelTable();
						try {
							// 2018-10-30
							// mTvMiscManager.setATVPresetFlag(false);//
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}, 1000);
				FactorySetting mfactorySetting = FactorySetting
						.getInstance(this);
				// CHMiscServiceBinder mCHMiscServiceBinder
				// =CHMiscServiceBinder.getInstance(mContext);;
				// if(mfactorySetting.getProductType() ==0){
				// mCHMiscServiceBinder.getSystemInfo().setSysModelString("changhong");
				// }else if(mfactorySetting.getProductType() ==1){
				// mCHMiscServiceBinder.getSystemInfo().setSysModelString("oboni");
				// }

				// zcj 2018-10-30
				// SystemProperties.set("persist.sys.product.model",mTvMiscManager.getProductName());

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		// .detectDiskReads()
		// .detectDiskWrites()
		// .detectNetwork() // or .detectAll() for all detectable problems
		// .penaltyLog()
		// .build());
		// StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
		// .detectLeakedSqlLiteObjects()
		// .penaltyLog()
		// .penaltyDeath()
		// .build());
		//

	}

	private boolean FilterKeyCode(int keyCode) {

		// zcj 2018-10-30 if(mTvMiscManager.getFactoryMode()){
		if (true) {
			return true;
		} else {

			switch (keyCode) {
			// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_FACTORY_DEDAULT:
			// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_M_MODE:
			// zcj 2018-10-31 case KeyEvent.KEYCODE_MENU:
			case KeyEvent.KEYCODE_BACK:
				// zcj 2018-10-31 case KeyEvent.KEYCODE_CHMIR_INPUT_SOURCE:
			case KeyEvent.KEYCODE_POWER:
				return true;
			default:
				return false;

			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		
		if (event.getRepeatCount() == 0 && Keycodevalue.length()<accessKeyCode.length()) {
			Keycodevalue = Keycodevalue + (keyCode - KeyEvent.KEYCODE_0);

			if (Keycodevalue.indexOf(accessKeyCode)!=-1) {
				Intent intent = new Intent();
				intent.setClass(FactoryActivity.this,
						FactoryExtraInfoActivity.class);
				startActivity(intent);
			}
		}
		else{
			Keycodevalue="";
		}

		// TODO Auto-generated method stub
		MenuItemData mData = mListMenu.getCurMenuItemData();
		if (mData != null && mData.equals(mPageData9.energySetting)) {
			mKeyCode[0] = mKeyCode[1];
			mKeyCode[1] = mKeyCode[2];
			mKeyCode[2] = keyCode;
		} else {
			mKeyCode[2] = 0;
		}
		int mScanCode=event.getScanCode();
		Log.v("FactoryActivity", "onKeyDown =" + keyCode);

		switch (keyCode) {
		case KeyEvent.KEYCODE_0:
			if (mData != null && mData.equals(mPageData1.projectID)) {
				changeProjectID(keyCode);
			}
			else{
				//updateOnePage(KeyEvent.KEYCODE_9 - KeyEvent.KEYCODE_0 + 1);
				updateOnePage(mPageData.length-1);
			}
			break;
		case KeyEvent.KEYCODE_1:
		case KeyEvent.KEYCODE_2:
		case KeyEvent.KEYCODE_3:
		case KeyEvent.KEYCODE_4:
		case KeyEvent.KEYCODE_5:
		case KeyEvent.KEYCODE_6:
		case KeyEvent.KEYCODE_7:
		case KeyEvent.KEYCODE_8:
		case KeyEvent.KEYCODE_9:
			if((keyCode- KeyEvent.KEYCODE_0)>(mPageData.length-1)){
				break;
			}
			if (mData != null && mData.equals(mPageData1.projectID)) {
				changeProjectID(keyCode);
			}
			else{
				updateOnePage(keyCode - KeyEvent.KEYCODE_1);
			}
			break;
		// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_FACTORY_DEDAULT:
		// zcj 2018-10-31 updateOnePage(KeyEvent.KEYCODE_7 -KeyEvent.KEYCODE_1);
		// zcj 2018-10-31 break;
		// case KeyEvent.KEYCODE_CHMIR_SSYS:
		// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_SCAN_PROG:
		// zcj 2018-10-31 updateOnePage(KeyEvent.KEYCODE_3 -KeyEvent.KEYCODE_1);
		// zcj 2018-10-31break;
		case KeyEvent.KEYCODE_3D_MODE:
			// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_INPUT_SOURCE:
		case KeyEvent.KEYCODE_SETTINGS:
			finish();
			break;
		case KeyEvent.KEYCODE_MENU:
			finish();
			break;
		// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_M_MODE:
		// zcj 2018-10-31if(time
		// ==0||((SystemClock.currentTimeMicro())-time)/1000> 500){
		// try {
		// if(mTvMiscManager.getFactoryMode() ==true){
		// mTvMiscManager.setFactoryMode(false);
		// Log.v("tv","setFactoryMode(false)");
		// }else{
		// Log.v("tv","FactoryMode ==false");
		// }
		//
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// FactoryModeMenu.refresh(this);
		// zcj 2018-10-31 Log.v("tv","close factorymenu");
		// finish();
		// }
		// zcj 2018-10-31 break;

		// zcj 2018-10-31case KeyEvent.KEYCODE_CHMIR_S_MODE:
		// zcj 2018-10-31 Log.v("tv","KeyEvent.KEYCODE_CHMIR_S_MODE");

		// new Handler().postDelayed(new Runnable() {
		//
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		// boolean factoryActive =false;
		// ActivityManager mActivityManager =
		// (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
		// List<ActivityManager.RunningTaskInfo> appList =
		// mActivityManager.getRunningTasks(100);
		// for (ActivityManager.RunningTaskInfo running : appList) {
		// Log.v("tv","factory1 ="+running.baseActivity.getClassName());
		// if("FactoryActivity".equals(running.baseActivity.getClassName()))
		// {
		// factoryActive=true;
		// break;
		// }
		//
		// }
		//
		// if(factoryActive){
		// Log.v("tv","exit factorymenu");
		// finish();
		// }
		//
		//
		// }
		// }, 500);

		// zcj 2018-11-5FactoryModeMenu.refresh(this);

		// zcj 2018-11-5 break;

		case KeyEvent.KEYCODE_DPAD_LEFT:
			if (mListMenu.getPageItem().isFocused()) {
				updateOnePage((curPageIndex + mPageData.length - 1)
						% mPageData.length);
			} else {
				if (mKeyCode[0] == KeyEvent.KEYCODE_DPAD_LEFT
						&& mKeyCode[1] == KeyEvent.KEYCODE_DPAD_LEFT
						&& mKeyCode[2] == KeyEvent.KEYCODE_DPAD_LEFT)
					if (mListMenu.getCurMenuItemData().equals(
							mPageData9.energySetting)) {
						//TODO mTitleName
						mPageData9.getNextPageList().get(0).mTitleName = strName[8];
						mPageData9.energySetting.mPassword = true;
						mPageData9.getNextPageList().get(0).mTotalPages = mPageData.length - 1;
						mListMenu.init(mPageData9.getNextPageList(), 0, 0);
					}
			}

			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			if (mListMenu.getPageItem().isFocused()) {
				updateOnePage((curPageIndex + 1) % mPageData.length);
			}
			break;

		case KeyEvent.KEYCODE_BACK:
			if(mScanCode == 158){
				if (mListMenu.getPageItem().isFocused()) {
					finish();
					break;
				} else {
					mListMenu.getPageItem().requestFocus();
				}
				return true;
			}else if(mScanCode == 174){
				finish();
				return true;
			}

		case KeyEvent.KEYCODE_INFO:
			//Start key pad Test
			Intent mIntent = new Intent();
			mIntent.setClass(mContext, FactoryKeypadTestActivity.class);
			mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(mIntent);
			break;

		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	PageData1 mPageData1;
	PageData2 mPageData2;
	static PageData3 mPageData3;
	PageData9 mPageData9;
	PageData0 mPageData0;

	void loadData() {
		strName = getResources().getStringArray(R.array.factory_index_array);
		mPageData0 = new PageData0(mContext, this);
		mPageData1 = new PageData1(mContext, this);
		mPageData2 = new PageData2(mContext, this);
		mPageData3 = new PageData3(mContext, this);
		PageData4 mPageData4 = new PageData4(this);
		PageData5 mPageData5 = new PageData5(mContext, this);
		PageData6 mPageData6 = new PageData6(this);
		PageData7 mPageData7 = new PageData7(mContext, this);
		PageData8 mPageData8 = new PageData8(this);
		mPageData9 = new PageData9(this);
		PageData10 mPageData10 = new PageData10(mContext, this);

		if(FactorySetting.isFactoryShowNoFunc()) {	
			if (isSuportDtv) {
				mPageData = new PageData[11];
			} else {
				mPageData = new PageData[10];
			}
		}else{
			mPageData = new PageData[9];
		}

		mPageData[0] = mPageData1;
		mPageData[1] = mPageData2;
		mPageData[2] = mPageData3;
		mPageData[3] = mPageData4;
		mPageData[4] = mPageData5;
		mPageData[5] = mPageData6;
		mPageData[6] = mPageData7;
		mPageData[7] = mPageData8;
		mPageData[8] = mPageData9;

		if(FactorySetting.isFactoryShowNoFunc()){
			if (isSuportDtv) {
				mPageData[9] = mPageData10;
			}

			mPageData[10] = mPageData0;
		}else{
			mPageData[8] = mPageData0;
		}

		String buildTime=SystemProperties.get("ro.vendor.build.date", "");
		String year=buildTime.substring(buildTime.length()-1);
		accessKeyCode="31761415"+year;
		
	}

	void updateOnePage(int pageIndex) {
		if(!FactorySetting.isFactoryShowNoFunc() && strName.length > mPageData.length){
			strName[mPageData.length-1]=strName[strName.length-1];
		}

		long startTime = System.currentTimeMillis();

		curPageIndex = pageIndex;
		List<MenuItemData> mListItemData = mPageData[pageIndex].LoadPageData();
		//TODO mTitleName
		mListItemData.get(0).mTitleName = strName[pageIndex];
		mListItemData.get(0).mTotalPages = mPageData.length - 1;
		mListMenu.init(mListItemData, (pageIndex + 1) % mPageData.length );

		//Log.d(TAG, "curPageIndex:"+curPageIndex);
		if(curPageIndex==(mPageData.length-1)){
			setHelpInfoVisible(true);
		}
		else{
			setHelpInfoVisible(false);
		}
		long endTime = System.currentTimeMillis();
		Log.v("wql3", "runtime:" + (endTime - startTime));
	}

	void updateOnePage(int pageIndex, int itemIndex) {

		if(!FactorySetting.isFactoryShowNoFunc() && strName.length > mPageData.length){
			strName[mPageData.length-1]=strName[strName.length-1];
		}
		curPageIndex = pageIndex;
		List<MenuItemData> mListItemData = mPageData[pageIndex].LoadPageData();
		//TODO mTitleName
		mListItemData.get(0).mTitleName = strName[pageIndex];
		mListItemData.get(0).mTotalPages = mPageData.length - 1;
		mListMenu.init(mListItemData, (pageIndex + 1) % mPageData.length , itemIndex);
		//Log.d(TAG, "curPageIndex:"+curPageIndex);
		if(curPageIndex==(mPageData.length-1)){
			setHelpInfoVisible(true);
		}
		else{
			setHelpInfoVisible(false);
		}
	}

	public void showMenu(boolean show) {
		mListMenu.showMenu(show);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (RESULT_OK == resultCode) {
			String mac = data.getStringExtra("macInfo");
			if (mac != null) {
				// comSettingManager
				// mcomSettingManager=comSettingManager.getInstance(null);
				// factorySetting factorySetting =
				// mcomSettingManager.getFactory();
				// factorySetting.setMAC(mac);
				mPageData2.updateMAC();
				Log.v("tv", "setMAC =" + mac);
			}	
		}
		if(requestCode==100){
			String panelname=data.getStringExtra("PANEL_NAME");
			mPageData1.panelSelect.updateValue(panelname);
		}
		Log.d(TAG, "requestCode="+requestCode+"resultCode="+resultCode);
		super.onActivityResult(requestCode, resultCode, data);
	}

	
	public static void initChannelTable() {
		if (mPageData3 != null) {
			mPageData3.initATVChannelTable();
		}
	}

	private String getSWVersion() {
		return mPageData1.getSWVersion();
	}

	private String getSWDate() {
		return mPageData1.getSWDate();
	}

	public void setHelpInfo(String info) {
		mListMenu.setHelpInfo(info);
	}
	public void setHelpInfoColor(int color) {
		mListMenu.setHelpInfoColor(color);
	}
	public void setHelpInfoVisible(boolean visible) {
		mListMenu.setHelpInfoVisible(visible);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (ItemInitIndex != -1) {
			updateOnePage(curPageIndex, -1);
			ItemInitIndex = -1;
		}
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		// factorySystemManager.removeUartColorTempNotifyLisenter(notifyLisenter);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void finish(){
		keypadControl(0);
		super.finish();
	}

	private int keypadControl(int value){
		int ret=MtkTvUtil.KeypadControl(value);
		Log.d(TAG,"keypadControl="+ret);
		return ret;
	}
	void isAgingMode(){
		int mode=0;
		mode= MtkTvFApiInformation.getInstance()
				.getIniParameter(Contants.KEYSTATUS_INI_FILEPATH,
						Contants.FACTORY_AGE_MODE).equalsIgnoreCase("1")?1:0;
		if(mode==1){
			int backlightInt=1;
			int dreamTimeInt=1;
			int twoHourInt=7200000;
			try{
				backlightInt = Settings.Global.getInt(mContext.getContentResolver(), "picture_backlight");
				//pictureModeInt=Settings.Global.getInt(mContext.getContentResolver(), "picture_mode");
				dreamTimeInt=Settings.System.getInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT);
			}catch (Settings.SettingNotFoundException e) {
				e.printStackTrace();
			}
			if(backlightInt!=100||dreamTimeInt!=twoHourInt){
				//Settings.Global.putInt(mContext.getContentResolver(), "picture_mode", 0);
				Settings.Global.putInt(mContext.getContentResolver(), "picture_backlight", 100);
				Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, twoHourInt);
			}

			MtkTvFApiDisplayTest.getInstance().setBurningMode(true);
			Intent intentForBarServer = new Intent(
					"FactoryModeService");
			intentForBarServer.setPackage("com.homwee.factory");
			mContext.startService(intentForBarServer);
		}
	}

	void initDataPath(String dataIndexPath){
		Log.d("zcj","Start initDataPath");
		String platform=SystemProperties.get("ro.board.platform", "");
		Map<String, Object> mDataIndex = FactoryIniReadService.readIni(dataIndexPath);
//        for(String key:mDataIndex.keySet()){
//            Map<String,String> data =(Map<String, String>)mDataIndex.get(key);
//            Log.d("zcj",key);
//            for(String k:data.keySet()){
//                Log.d("zcj",data.get(k));
//            }
//        }
		if(mDataIndex==null || mDataIndex.size()==0){
			Log.d(TAG,"mDataIndex is null,Path="+dataIndexPath);
		}else {
			Contants.PATH_FACTORYINI = ((Map<String, String>) mDataIndex.get("Factory")).get("m_pFactoryRecord_File");
			Contants.PATH_CUSTOM_FUNC_INI = ((Map<String, String>) mDataIndex.get("Misc")).get("m_pCustomizationFunc_File");
			Contants.PATH_PROJECT_INFO = ((Map<String, String>) mDataIndex.get("model")).get("m_pModeInfo_File");
			Contants.PATH_INPUT_SOURCE = ((Map<String, String>) mDataIndex.get("InputSource")).get("m_pInputSourceConfig_File");
			Contants.PATH_IR_CONFIG = ((Map<String, String>) mDataIndex.get("ir")).get("m_pIrConfig_File");
			Contants.FACTORY_PAGE2_PQ_PATH = ((Map<String, String>) mDataIndex.get("panel")).get("PQBinPathName");
			String FACTORY_PAGE2_PQGENERAL_COLOR = ((Map<String, String>) mDataIndex.get("PQ_general")).get("m_pIniColorTuner_File");
			Contants.FACTORY_PAGE2_PQGENERAL_PATH = FACTORY_PAGE2_PQGENERAL_COLOR.substring(0, FACTORY_PAGE2_PQGENERAL_COLOR.lastIndexOf("/") + 1);
			Contants.FACTORY_PAGE2_AQ_PATH = ((Map<String, String>) mDataIndex.get("AQ")).get("AQPathName");
			Contants.FACTORY_PAGE2_COLORMA_PATH = ((Map<String, String>) mDataIndex.get("ColorMatrix")).get("MatrixName");
			Contants.FACTORY_PAGE2_COLORMA_PATH = Contants.FACTORY_PAGE2_COLORMA_PATH.substring(0,Contants.FACTORY_PAGE2_COLORMA_PATH.lastIndexOf("/")+1);
			Contants.FACTORY_PAGE2_PANEL_INI_PATH = ((Map<String, String>) mDataIndex.get("panel")).get("m_pPanelName");
			Contants.FACTORY_PAGE2_PANEL_PATH = Contants.FACTORY_PAGE2_PANEL_INI_PATH.substring(0, Contants.FACTORY_PAGE2_PANEL_INI_PATH.lastIndexOf("/") + 1);
			String FACTORY_PAGE2_MODEL_PATH_CUSTOMER1INI=((Map<String, String>) mDataIndex.get("model")).get("m_customerIniFile");
			Contants.FACTORY_PAGE2_MODEL_PATH = FACTORY_PAGE2_MODEL_PATH_CUSTOMER1INI.substring(0,FACTORY_PAGE2_MODEL_PATH_CUSTOMER1INI.lastIndexOf("/")+1);
			Contants.FACTORY_PAGE2_EDIDBIN_PATH = ((Map<String, String>) mDataIndex.get("EDID")).get("m_pEdidBinPath");
			Contants.FACTORY_PAGE2_HDRBIN_INI_PATH = ((Map<String, String>) mDataIndex.get("Hdr")).get("m_pIniFile");
			Contants.FACTORY_PAGE2_HDRBIN_PATH = Contants.FACTORY_PAGE2_HDRBIN_INI_PATH.substring(0, Contants.FACTORY_PAGE2_HDRBIN_INI_PATH.lastIndexOf("/")+1);
			Contants.FACTORY_PAGE2_MODULE_NAME_PATH = ((Map<String, String>) mDataIndex.get("module")).get("m_pModuleName");
			Contants.FACTORY_PAGE2_MODULE_PATH = Contants.FACTORY_PAGE2_MODULE_NAME_PATH.substring(0, Contants.FACTORY_PAGE2_MODULE_NAME_PATH.lastIndexOf("/") + 1);
			Contants.FACTORY_PAGE2_CUSTOMER_PATH = ((Map<String, String>) mDataIndex.get("model")).get("m_customerIniFile");
			Contants.FACTORY_PAGE1_AQ_VERSION_PATH = Contants.FACTORY_PAGE2_AQ_PATH + "AQ_Version.ini";
			Contants.FACTORY_EXTRA_BOARD_PATH = ((Map<String, String>) mDataIndex.get("board")).get("m_pBoardName");
			//for tvtools export
			if(platform.equals("m5621") || platform.equals("m7322")) {
            	Contants.FACTORY_TVTOOLS_INPUTSOURCEPATH = "/vendor/tvconfig/config/aq/";
	            Contants.FACTORY_TVTOOLS_SOUNDSTYLEPATH = "/vendor/tvconfig/config/aq/";
	            Contants.FACTORY_TVTOOLS_OSDMAPPINGPATH = "/vendor/tvconfig/config/PQ_general/OsdMapping.ini";
	            Contants.FACTORY_TVTOOLS_NLAPATH = "/vendor/tvconfig/config/PQ_general/NLA.ini";
	            Contants.FACTORY_TVTOOLS_OVERSCANPATH = "/vendor/tvconfig/apollo/overscan.ini";
            }else {
	            Contants.FACTORY_TVTOOLS_INPUTSOURCEPATH = ((Map<String, String>) mDataIndex.get("AQ")).get("AqInputSrcPathName");
	            Contants.FACTORY_TVTOOLS_SOUNDSTYLEPATH = ((Map<String, String>) mDataIndex.get("AQ")).get("AqSoundStylePathName");
	            Contants.FACTORY_TVTOOLS_OSDMAPPINGPATH = ((Map<String, String>) mDataIndex.get("PQ_general")).get("m_pIniOsdMapping_File");
	            Contants.FACTORY_TVTOOLS_NLAPATH = ((Map<String, String>) mDataIndex.get("PQ_general")).get("m_pIniNLA_File");
	            Contants.FACTORY_TVTOOLS_OVERSCANPATH = ((Map<String, String>) mDataIndex.get("Overscan")).get("m_pOverscan_File");
            }
            
            if(mDataIndex.containsKey("CustomerDataVersion")) {
                Contants.FACTORY_DATAINDEX_DATAVERSION = ((Map<String, String>) mDataIndex.get("CustomerDataVersion")).get("m_DataVersion");
            }
			if(((Map<String,String>) mDataIndex.get("panel")).containsKey("HSYPQBinPathName")) {
				Contants.FACTORY_PAGE1_HSYPQBIN_PATH = ((Map<String, String>) mDataIndex.get("panel")).get("HSYPQBinPathName");
			}
			Contants.FACTORY_BOOTLOGOCFG_FILE=((Map<String, String>) mDataIndex.get("BootLogoMusic")).get("m_pBootLogoMusicCfg_File");
			if(((Map<String,String>) mDataIndex.get("Factory")).containsKey("m_pAutoEnterFactory_File")) {
				Contants.PATH_FACTORY_AUTOENTERFACTORY_INI = ((Map<String, String>) mDataIndex.get("Factory")).get("m_pAutoEnterFactory_File");
			}
			if(((Map<String,String>) mDataIndex.get("TCON_BIN")).containsKey("m_pIniTconCfgFile")) {
				Contants.FACTORY_TCONCFGFILE = ((Map<String, String>) mDataIndex.get("TCON_BIN")).get("m_pIniTconCfgFile");
			}
			

			try {
				Contants.FACTORY_DATAINDEX_HASHKEY_SELECT = ((Map<String, String>) mDataIndex.get("CustomerDataVersion")).get("m_hashkeySelect");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
}